/*******************************************************************************
 * Copyright（C） CETC-32
 * @file:LoginUser.java
 * @Description：
 * @Author ：徐文远
 * @version：1.0
 * @date ： 2021/2/2 下午1:56
 ******************************************************************************/

package com.cetc32.webutil.common.bean;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class LoginUser {

    /**
     * 用户ID
     * **/
    private int id;

    /**
     * 用户名
     * **/
    private String username;

    /***
     *
     * **/
    private String realName;

    /**
     * 用户角色
     * */
    private List<Integer> role;

    /**
     * 用户安全等级
     * **/
    private Integer security;

    /**
     * 用户部门
     * **/
    private List<Integer> department;

    /**
     * 用户区域权限
     * **/
    private List<String> areacode;

    /**
     * 用户权限
     * **/
    private List<String > permissions;

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public List<Integer> getRole() {
        return role;
    }

    public void setRole(List<Integer> roles){
        role=roles;
    }
    public void setRole(Integer[] roles){
        if(null!= roles)
        role=Arrays.asList(roles);
    }
    public void setRole(String role)
    {
        this.role=str2intList(role);
    }

    public List<Integer> getDepartment() {
        return department;
    }
    public void setDepartment(Integer[] dept){
        if(dept!=null)
            this.department=Arrays.asList(dept);
    }
    public void setDepartment(List<Integer> dept){
        this.department=dept;
    }

    public void setDepartment(String department)
    {
        this.department=str2intList(department);
    }

    public List<String> getAreacode() {
        return areacode;
    }
    public void setAreacode(String[] str){
        if(str!=null)
        this.areacode=Arrays.asList(str);
    }
    public void setAreacode(List<String> acode){
        this.areacode=acode;
    }
    public void setAreacode(String areacode)
    {
        if(areacode == null)
        {
            this.areacode = new ArrayList<>();
        }
        String s=trimBothEndsChars(areacode,",");
        this.areacode= Stream.of(s.split(",")).collect(Collectors.toList());
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getSecurity() {
        return security;
    }

    public void setSecurity(Integer security) {
        this.security = security;
    }

    private List<Integer> str2intList(String str)
    {
        if(str==null)
        {
            return new ArrayList<>();
        }
        String s=trimBothEndsChars(str,",");
        return Stream.of(s.split(",")).map(Integer::parseInt).collect(Collectors.toList());
    }


    public void setPermissions(String[] permissions){
        if(permissions ==null)
        {
            this.permissions = new ArrayList<>();
            return;
        }
        if(permissions instanceof String[])
        {
            if(permissions.length==0)
            {
                return;
            }
            if(this.permissions==null )
                this.permissions=new ArrayList<>();
            this.permissions.clear();
            this.permissions.addAll(Arrays.asList(permissions));
        }
    }
    public void setPermissions(List<String> permissions){
        if(permissions ==null)
        {
            this.permissions = new ArrayList<>();
            return;
        }
        if(permissions instanceof List)
        {
            if(permissions.isEmpty())
            {
                //this.permissions = new ArrayList<>();
                return;
            }
            if(this.permissions==null )
                this.permissions=new ArrayList<>();
            this.permissions.clear();
            this.permissions.addAll(permissions);
        }
    }
    public void setPermissions(String permissions){
        if(permissions ==null)
        {
            this.permissions = new ArrayList<>();
            return;
        }
        if(permissions instanceof String)
        {
            if(permissions.isEmpty())
            {
                //this.permissions = new ArrayList<>();
                return ;
            }
            String s=trimBothEndsChars(permissions,",");
            this.permissions=Stream.of(s.split(",")).collect(Collectors.toList());
        }
    }
    public List<String> getPermissions() {
        return permissions;
    }
    public boolean hasPermision(String p){
        if(p==null || p.isEmpty())
            return false;
        if(permissions==null ||permissions.size()==0)
            return false;
        for(String s:permissions){
            if(s.equals(p))
                return true;
        }
        return false;
    }
    public boolean hasPermision(List<String> ps){
        if(ps==null || ps.size()==0)
            return false;
        if(permissions==null ||permissions.size()==0)
            return false;
        for(String p:ps)
            for(String s:permissions){
                if(s.equals(p))
                    return true;
            }
        return false;
    }
    public boolean isAdminOf(String sysCode){
        if(permissions!=null && permissions.size()>0 && StringUtils.isNotBlank(sysCode)){
            return permissions.stream().anyMatch(f->f.startsWith(sysCode+"*"));
        }
        return false;
    }
    public boolean hasPermision(String[] ps){
        if(ps==null || ps.length==0)
            return false;
        if(permissions==null ||permissions.size()==0)
            return false;
        for(String p:ps)
            for(String s:permissions){
                if(s.equals(p))
                    return true;
            }
        return false;
    }

    /**
     * 去除字符串首尾两端指定的字符
     * */
    private static String trimBothEndsChars(String srcStr, String splitter) {
        String regex = "^" + splitter + "*|" + splitter + "*$";
        return srcStr.replaceAll(regex, "");
    }
}
